/**
 * gislin 2017年7月19日
 */
package com.btcode.db.conver;

import com.btcode.exception.MsgException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author gislin 2017年7月19日
 */
public class DefaultValueConver implements IValueConver, Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /**
     * 保证simpleDateFormat线程安全
     */
    private static ThreadLocal<SimpleDateFormat> sfPool = new ThreadLocal<SimpleDateFormat>();

    @Override
    public Integer converToInteger(Object value) {
        Integer result = null;
        if (value == null) {
            return null;
        }
        if (value instanceof Integer) {
            result = (Integer) value;
        }
        else if (value instanceof Long) {
            result = ((Long) value).intValue();
        }
        else if (value instanceof Double) {
            result = ((Double) value).intValue();
        }
        else {
            try {
                String regEx = "([0-9]+)";
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(value.toString());
                m.find();
                result = Integer.parseInt(m.group());
            }
            catch (Exception e) {

            }
        }
        return result;
    }

    @Override
    public String converToString(Object value) {
        if (value == null) {
            return null;
        }

        return value.toString();
    }

    @Override
    public Double converToDouble(Object value) {
        Double result = null;
        if (value == null) {
            return null;
        }
        if (value instanceof Double) {
            result = (Double) value;
        }
        else {
            try {
                result = Double.parseDouble(value.toString());
            }
            catch (Exception e) {
                throw new MsgException("类型转换失败", e);
            }
        }
        return result;
    }

    @Override
    public Date converToDate(Object value) {
        Date result = null;

        if (value == null) {
            return null;
        }

        if (value instanceof Date) {
            result = (Date) value;
        }
        else if (value instanceof java.sql.Date) {
            java.sql.Date sqlDate = (java.sql.Date) value;
            result = new Date(sqlDate.getTime());
        }
        else if (value instanceof Timestamp) {
            Timestamp timestamp = (Timestamp) value;
            result = new Date(timestamp.getTime());
        }
        else if (value instanceof Long) {
            Long lvalue = new Long(value.toString());
            result = new Date(lvalue);
        }
        else if (value instanceof Integer) {

        }
        else {
            try {
                try{
                    SimpleDateFormat sf = sfPool.get();
                    if (sf == null) {
                        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        sfPool.set(sf);
                    }
                    result = sf.parse(value.toString());
                }
                catch (Exception e){
                    Long longValue = Long.parseLong(value.toString());
                    result = new Date(longValue);
                }
            }
            catch (Exception e) {
                throw new MsgException("类型转换失败", e);
            }
        }
        return result;
    }

    @Override
    public BigDecimal converToBigDecimal(Object value) {
        BigDecimal result = null;
        if (value == null) {
            return null;
        }
        if (value instanceof BigDecimal) {
            return (BigDecimal) value;
        }
        result = BigDecimal.valueOf(Long.parseLong(value.toString()));
        return result;
    }

    @Override
    public Timestamp converToDateTime(Object value) {
        Date dateResult = converToDate(value);
        if (dateResult == null) {
            return null;
        }
        Timestamp result = new Timestamp(dateResult.getTime());
        return result;
    }

}
